//
//  MyStockRadarEditControll.m
//  Stock
//
//  Created by blacksun on 11-7-8.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStockRadarEditControll.h"


@implementation MyStockRadarEditControll
@synthesize tableView;
@synthesize pickerView;
@synthesize radar;
@synthesize values;
@synthesize myStockService;


- (void)dealloc
{
    [tableView release];
    [pickerView release];
    [values release];
    [myStockService cancleAllRequest];
    [myStockService release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    lastStatus = radar.status;
    [[StockAppHelper shareInstance] showAlertViewWithMessage:@"保存成功" andDelegate:self];
    [radar save];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    radar.status = lastStatus;
    [[StockAppHelper shareInstance] showAlertViewWithMessage:@"保存失败"];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
    NSLog(@"cancle");
    [self.navigationController popViewControllerAnimated:YES];
}
/*- (void)alertViewCancel:(UIAlertView *)alertView{
    NSLog(@"cancle2");
}*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"cancle3");
}
-(void)saveStockRadar{
    radar.status = 2;
    [myStockService uploadStockRadar:radar];
}
-(void)switchViewValueDidChanged:(id)sender{
    UISwitch *switchView = (UISwitch *)sender;
    radar.repeat = switchView.on;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    lastStatus = radar.status;
    self.myStockService = [[[MyStockService alloc] initWithDelegate:self] autorelease];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveStockRadar)];
    
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
    
    self.values = [NSMutableArray array];
    float price = [radar.currentValue floatValue];
    
    if (radar.type == kStockMonitorTypeUp) {
        for (int i=0; i<101; i++) {
            [values addObject:[NSString stringWithFormat:@"%.2f",price*(1+0.01*i)]];
        }
    }else{
        for (int i=0; i<101; i++) {
            [values addObject:[NSString stringWithFormat:@"%.2f",price*(1-0.01*i)]];
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [tableView release];
    [pickerView release];
    [values release];
    [myStockService cancleAllRequest];
    [myStockService release];
 
}

#pragma mark - tableView 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"stockRadarCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(190.0, 12.0, 120.0, 20.0)];
        rightLabel.backgroundColor = [UIColor clearColor];
        rightLabel.tag = 1000;
        rightLabel.textColor = [UIColor whiteColor];
        rightLabel.textAlignment = UITextAlignmentRight;
        
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(216.0, 8.0, 94.0, 27.0)];
        [switchView addTarget:self action:@selector(switchViewValueDidChanged:) forControlEvents:UIControlEventValueChanged];
        switchView.tag = 101;
        
                
        [cell.contentView addSubview:rightLabel];
        [cell.contentView addSubview:switchView];
        [rightLabel release];
        [switchView release];
    }
    
    NSInteger row = indexPath.row;
    NSString *imageName = @"cellbg1.png";
    if (row%2==1) {
        imageName = @"cellbg2.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    UILabel *rightLabel = (UILabel *)[cell viewWithTag:1000];
    UISwitch *switchView = (UISwitch *)[cell viewWithTag:101];
    rightLabel.hidden = YES;
    switchView.hidden = YES;
    
    
    if (row==0) {
        cell.textLabel.text = @"当前价";
        rightLabel.hidden = NO;
        rightLabel.text = radar.currentValue;
    }else if (row==1) {
        cell.textLabel.text = @"预警阀值";
        rightLabel.hidden = NO;
        rightLabel.text = radar.targetValue;
    }/*else if (row==2) {
        cell.textLabel.text = @"预警差幅";
        rightLabel.hidden = NO;
        rightLabel.text = radar.targetPercent;
    }else if (row==2) {
        cell.textLabel.text = @"重复提醒";
        switchView.hidden = NO;
        switchView.on = radar.repeat;
    }*/ 
    return cell;
}

#pragma mark -PickerView 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (radar.type == kStockMonitorTypeUp) {
        return 101;
    }
    return 101;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [values objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    radar.targetValue = [values objectAtIndex:row];
    radar.targetPercent = [NSString stringWithFormat:@"%d%%",row*1];
    [self.tableView reloadData];
}
@end
